import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_instance/src/extension_instance.dart';
import 'package:get/get_navigation/src/extension_navigation.dart';
import 'package:meiyunapp/database/table/user_table.dart';
import 'package:meiyunapp/page/main/child/my_page.dart';
import 'package:meiyunapp/page/main/child/order_page.dart';
import 'package:meiyunapp/page/main/child/home_page.dart';
import 'package:meiyunapp/service/db_services.dart';
import 'package:meiyunapp/service/mqtt_services.dart';
import 'package:meiyunapp/service/store_services.dart';
import 'package:meiyunapp/utils/android_back_desktop.dart';
import 'package:meiyunapp/widget/app_bottom_app_bar.dart';

class MainPage extends StatefulWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  List<Widget> pages = const [MinePage(), OrderPage(), MinePage(), OrderPage()];

  final pageController = PageController(initialPage: 0);
  int _index = 0;

  void onPageChanged(int index) {
    setState(() {
      _index = index;
    });
  }

  @override
  void initState() {
    super.initState();
    var argument = Get.arguments["name"];
    print("argument:$argument");
    StoreService.to.put("name", "!23");
  }

  @override
  void dispose() {
    pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: AndroidBackDesktop.backToDesktop,
      child: Scaffold(
        body: PageView(
          controller: pageController,
          onPageChanged: onPageChanged,
          children: pages,
          physics: const NeverScrollableScrollPhysics(), // 禁止滑动
        ),
        bottomNavigationBar: AppBottomAppBar(
          selectedIndex: _index,
          itemClickCallback: (int position) {
            pageController.jumpToPage(position);
          },
          showFloatFabLocation: true,
          showElevation: true,
          items: [
            BottomBarItem(
              title: const Text("主页"),
              mIcon: const Icon(Icons.home_filled),
              activeColor: Colors.blue,
            ),
            BottomBarItem(
              title: const Text("订单"),
              mIcon: const Icon(Icons.shop),
              activeColor: Colors.blue,
            ),
            BottomBarItem(
              title: const Text("消息"),
              mIcon: const Icon(Icons.message),
              activeColor: Colors.blue,
              badge: 1,
            ),
            BottomBarItem(
              title: const Text("我的"),
              mIcon: const Icon(Icons.perm_identity),
              activeColor: Colors.blue,
              badge: 1,
            ),
          ],
        ),
        floatingActionButtonLocation: AlmostEndFloatFabLocation(
          FloatingActionButtonLocation.miniCenterDocked,
          offsetY: 20.h,
          offsetX: 0,
        ),
        floatingActionButtonAnimator: NoScalingAnimation(),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            showModalBottomSheet(
              backgroundColor: Colors.transparent,
              context: context,
              builder: (context) {
                return Container(
                  width: double.infinity,
                  height: 400.h,
                  decoration: const BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(15),
                      topRight: Radius.circular(15),
                    ),
                  ),
                );
              },
            );
          },
          child: const Icon(Icons.qr_code),
        ),
      ),
    );
  }
}
